package serviceimpl;


import dao.AdminDao;
import dao.UserDao;
import daoimpl.AdminDaoImpl;
import daoimpl.UserDaoImpl;
import javabean.Admin;
import javabean.User;
import service.AdminModuleService;

import java.util.ArrayList;

public class AdminModuleServiceImpl implements AdminModuleService {
    //创建服务需要操作数据库的dao对象
    private final UserDao<User> userDao = new UserDaoImpl();
    private final AdminDao<Admin> adminDao = new AdminDaoImpl();


    @Override
    public boolean createAdmin(String username, String password, String name, String email, String detailed) {
        Admin admin = new Admin(username, password, name, email, detailed);
        int insert = adminDao.insert(admin);
        return insert == 1;
    }

    @Override
    public boolean createUser(String username, String password, String name, String email, double money) {
        User user = new User(username, password, name, email, money);
        int insert = userDao.insert(user);
        return insert == 1;
    }

    @Override
    public boolean removeAdmin(int id) {
        int i = adminDao.deleteById(id);
        return i == 1;
    }

    @Override
    public boolean removeUser(int id) {
        int i = userDao.deleteById(id);
        return i == 1;
    }

    @Override
    public boolean userResetUserPassword(int uid, String password) {
        int i = userDao.updatePasswordById(uid, password);
        return i == 1;
    }

    @Override
    public boolean adminResetPassword(int aid, String password) {
        int i = adminDao.updatePasswordById(aid, password);
        return i == 1;
    }

    @Override
    public ArrayList<Admin> findAllAdmin() {

        return adminDao.selectAll();
    }

    @Override
    public Admin findMyAdminByUsername(String username) {
        ArrayList<Admin> admins = adminDao.selectAdminByUsername(username);
        if(admins.isEmpty()) {
            return null;
        } else {
            return admins.get(0);
        }
    }

    @Override
    public ArrayList<User> findAllUser() {

        return userDao.selectAll();
    }
}